package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

public class VitalityArcProgress extends View {

    /**
     * 画笔
     * */
    private Paint mPaint;
    /**
     * 文字画笔
     * */
    private Paint mTextPaint;
    /**
     * 文字画笔
     * */
    private Paint mSmallTextPaint;
    /**
     * 填充进度颜色
     * */
    private int insideColor;
    /**
     * 背景颜色
     * */
    private int outsideColor;
    /**
     * 文字颜色
     * */
    private int textColor;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    /**
     * 当前进度条
     * */
    private int currentProgress = 50;

    /**
     * 文字的高度
     * */
    private int textheight;

    /**
     * 弧线的宽度
     * */
    private float arcStrokeWidth;

    /**
     * 最大进度
     * */
    private int maxCount = 100;

    private String level = "100分";

    public VitalityArcProgress(Context context) {
        this(context,null);
    }

    public VitalityArcProgress(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public VitalityArcProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        arcStrokeWidth = DensityUtil.dpToPx(6);
        maxCount = 100;
        outsideColor = Color.parseColor("#e9e9e9");
        insideColor = Color.parseColor("#5dc6ff");
        textColor = Color.parseColor("#292d30");
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(DensityUtil.dpToPx(18));
        textheight = (int)(mTextPaint.ascent()+mTextPaint.descent());
        mSmallTextPaint = new Paint();
        mSmallTextPaint.setAntiAlias(true);
        mSmallTextPaint.setTextSize(DensityUtil.dpToPx(14));
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
        if(width>height){
            width = height;
        }else {
            height = width;
        }
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = 500;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawInside(canvas);
        drawOutside(canvas,currentProgress);
        drawText(canvas,level);
    }

    /**
     * 画字
     * */
    private void drawText(Canvas canvas,String level) {
        if(TextUtils.isEmpty(level)){
            level = "--";
        }
        mTextPaint.setColor(textColor);
        int textWidth = (int)mTextPaint.measureText(level);
        canvas.drawText(level,0,level.length(),width/2-textWidth/2,(height-textheight)/2,mTextPaint);
    }
    /**
     * 画进度
     * */
    private void drawOutside(Canvas canvas,int progress) {
        float angle;
        if((progress/(maxCount*1f))*270>270){
            angle = 270;
        }else {
            angle =  (progress/(maxCount*1f))*270;
        }
        LinearGradient paint =new LinearGradient(0,height/2,width,height/2,new int[]{Color.parseColor("#3ec1c9"),insideColor},new float[]{0,1},Shader.TileMode.CLAMP);
        mPaint.setShader(paint);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(arcStrokeWidth);
        RectF rectF = new RectF(arcStrokeWidth+5,arcStrokeWidth+5,width-arcStrokeWidth-5,height-arcStrokeWidth-5);
        canvas.drawArc(rectF,135f,angle,false,mPaint);
        mPaint.setShader(null);


        mPaint.setStyle(Paint.Style.FILL);

        float radius = rectF.width()/2;
        mPaint.setColor(insideColor);
        float x =(float) (width/2+radius*Math.cos((135f+angle)*Math.PI/180));
        float y =(float) (width/2+radius*Math.sin((135f+angle)*Math.PI/180));

        canvas.drawCircle(Math.abs(x),
                Math.abs(y),
                16f,mPaint);
        mPaint.setColor(Color.WHITE);
        canvas.drawCircle(Math.abs(x),
                Math.abs(y),
                10f,mPaint);

    }
    /**
     * 画背景
     * */
    private void drawInside(Canvas canvas) {
        mPaint.setColor(outsideColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(arcStrokeWidth);
        RectF rectF = new RectF(arcStrokeWidth+5,arcStrokeWidth+5,width-arcStrokeWidth-5,height-arcStrokeWidth-5);
        canvas.drawArc(rectF,0f,360f,false,mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width/2,height/2,width/3,mPaint);
    }

    public void setProgress(int currentProgress) {
        this.currentProgress = currentProgress;
        invalidate();
    }
}
